package main

import (
	"bbs-web/internal/ioc"
	"flag"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"net/http"
	"time"
)

/*
对标网站 https://mlog.club/
github https://github.com/mlogclub/bbs-go
*/

var configFile = flag.String("config", "etc/dev.yaml", "配置文件路径")

func main() {
	app := InitWebServer(*configFile)
	engine := app.server
	initPrometheus()
	ioc.SetUpOTEL(ioc.AppConfig)

	//启动定时任务
	app.cron.Start()

	// 启动kafka消费者，
	for _, consumer := range app.consumers {
		err := consumer.Start()
		if err != nil {
			panic(err)
		}
	}

	engine.Run(":8282")

	//退出定时任务
	ctx := app.cron.Stop()
	tm := time.NewTimer(time.Minute * 10)
	select {
	case <-tm.C:
	case <-ctx.Done(): // 可以考虑超时强制退出 防止有些任务执行特别长的时间

	}
}

func initPrometheus() {
	go func() {
		http.Handle("/metrics", promhttp.Handler())
		// 监听端口，可以做成可配置的
		http.ListenAndServe(":8899", nil)
	}()
}

//config := ioc.InitConfig(*configFile)
//db := ioc.InitDatabase(config)
//cmdable := ioc.InitRedis(config)
//InitSaramaClient := ioc.InitSaramaClient(config)
//SyncProducer := ioc.InitSyncProducer(InitSaramaClient)
//logger := ioc.InitLogger()
//articleCache := cache.NewArticleCache(cmdable)
//producer := article1.NewProducer(SyncProducer)
//iUserDao := dao.NewUserDao(db)
//articleDAO := article_dao.NewGormArticleDao(db)
//iUserRepo := use.NewUserRepo(iUserDao)
//interactiveDao := dao.NewInteractiveDao(db)
//redisInteractiveCache := cache.NewRedisInteractiveCache(cmdable)
//interactiveRepo := repository.NewInteractiveRepo(interactiveDao, redisInteractiveCache, logger)
//interactiveService := service.NewInteractiveService(interactiveRepo, redisInteractiveCache)
//articleRepository := article5.NewArticleRepo(articleDAO, articleCache, logger, iUserDao)
//iUserService := service.NewUserService(iUserRepo)
//icaptchaService := service.NewCaptchaService()
//captchaHandler := handler.NewCaptchaHandler(icaptchaService)
//jwtHandler := jwtx.NewRedisJWTHandler(cmdable)
//userHandler := handler.NewuserHandler(iUserService, icaptchaService, jwtHandler)
//iArticleService := article3.NewArticleService(articleRepository, logger, producer)
//articleHandler := handler.NewArticleHandler(iArticleService, logger, interactiveService)
//router := web.NewRouter(userHandler, captchaHandler, articleHandler)
//v := ioc.InitMiddleware(config, jwtHandler)
//engine := ioc.InitGin(router, v)
////consumer := article1.NewInteractiveReadEventBatchConsumer(InitSaramaClient, logger, interactiveRepo)
////v2 := ioc.InitConsumer(consumer)
//initPrometheus()
//ioc.SetUpOTEL(ioc.AppConfig)
